package com.tao.aszx.manager.service.Impl;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.tao.aszx.manager.service.ValidateCodeService;
import com.tao.aszx.model.vo.system.ValidateCodeVo;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


@Service
public class ValidateCodeServiceImpl implements ValidateCodeService {



    @Resource
    private RedisTemplate<String , String> redisTemplate ;

    @Override
    public ValidateCodeVo generateValidateCode() {
        // 使用hutool工具包中的工具类生成图片验证码
        //参数：宽  高  验证码位数 干扰线数量
        CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(150, 48, 4, 30);
        // 获取图像验证码
        String codeValue = circleCaptcha.getCode();
        String codeImage= circleCaptcha.getImageBase64();

        // 生成uuid作为图片验证码的key
        String codeKey = java.util.UUID.randomUUID().toString().replace("-","");

        // 将验证码存储到Redis中
        redisTemplate.opsForValue().set("user:login:validatecode:" + codeKey,codeValue,5,java.util.concurrent.TimeUnit.MINUTES);
        // 构建响应结果数据
        ValidateCodeVo validateCodeVo = new ValidateCodeVo(codeKey,"data:image/png;base64,"+codeImage);

        return validateCodeVo;
    }
}
